<template>
  <div class="cesium-viewer-wapper">
    <div id="cesiumContainer" />
    <viewerSettingSidebar top="20em" :viewerSource="viewerSource" />
    <div class="tool-box-top-left">
      <!-- <div class="select-box-wrapper"><select-box :options="flyOptions" style="margin-right: 0;" /></div> -->
      <div class="select-box-wrapper"><a-checkbox v-model:checked="viewModel.translucencyEnabled">translucencyEnabled</a-checkbox></div>
      <div class="select-box-wrapper"><a-checkbox v-model:checked="viewModel.fadeByDistance">fadeByDistance</a-checkbox></div>
      <div class="select-box-wrapper"><a-checkbox v-model:checked="viewModel.showVectorData">showVectorData</a-checkbox></div>
      <div class="select-box-wrapper"><SliderBar v-model:value="viewModel.alpha" :min="0" :max="1" :step="0.1">alpha</SliderBar></div>
    </div>
    <GlobeRotater showTime :viewerSource="viewerSource" />
    <MousePosition show :viewerSource="viewerSource" />
  </div>
</template>

<script>
import * as Cesium from '@cesiumjs';
import { defineComponent, ref, reactive, watch, toRaw } from 'vue';
import { SelectBox, SliderBar } from '@components/widgets';
import { MousePosition, viewerSettingSidebar, GlobeRotater } from '@g3x/g3v/ui';
import { G3V } from '@g3x';
import {viewerMixin} from '@mixins';
import merge from '@g3x/utils/merge';

const $g3vOptions = {
  // defaultViewRectangle: G3V.defaultViews.global.china.rectangle,
  viewers: [
    {
      dom: 'cesiumContainer',
      name: 'viewer1',
      title: 'viewer#01',
      viewer: {
        imagery: { type: 'arcgisImagery', options: { brightness: 1, show: true } },
        imageryProvider: null,
        terrain: { type: 'default', options: { requestVertexNormals: true, requestWaterMask: true } },
        terrainProvider: null,
        shadows: false,
        scene3DOnly: true,
        // sceneMode: Cesium.SceneMode.SCENE2D,
        animation: false
      },
      layers: [
        { type: 'osmImagery', options: { brightness: 1, show: false } },
        { type: 'arcgisStreetMap', options: { brightness: 1, show: false } },
        { type: 'bingMapsImagery', options: { brightness: 1, show: false } },
        { type: 'tdtIboMap', options: { brightness: 1, show: false } },
        { type: 'tdtImgAnno', options: { brightness: 1, show: false } },
        {
          type: 'graticule',
          options: {
            brightness: 1,
            show: true
          }
        }
      ],
      globe: {
        depthTestAgainstTerrain: true,
        enableLighting: false //启用以太阳为光源的地球
      },
      bottomContainer: false,
      navigation: {
        // defaultResetView: G3V.defaultViews.global.china.rectangle ,
        defaultResetView: null //flyHome
      },
      shortcutAction: false,
      hideLayers: false
    }
  ]
};

export default defineComponent({
  name: 'ParcLeadMine', // Parc铅矿
  mixins: [viewerMixin],
  components: { SelectBox, SliderBar, viewerSettingSidebar, GlobeRotater, MousePosition },
  props: {},
  setup() {
    var viewModel = reactive({
      translucencyEnabled: true,
      fadeByDistance: true,
      showVectorData: false,
      alpha: 0.5
    });
    return { viewModel };
  },
  data() {
    return {
      flyOptions: []
    };
  },
  computed: {},
  watch: {},
  methods: {
    g3vOptionsGet(original) {
      // const g3vOptions = merge($g3vOptions, true);
      return $g3vOptions;
    },
    onViewerInited() {
      console.log('gdp onViewerInited');
    },
    initEffects() {
      const vm = this;
      const { viewer } = vm;
      const scene = viewer.scene;
      const globe = scene.globe;

      var tileset = new Cesium.Cesium3DTileset({
        url: '//zzgis.com/cdn/3d/3dtiles/mapbox_3dtiles_samples/animations/tileset.json',
        url: '//zzgis.com/cdn/3d/3dtiles/mapbox_3dtiles_samples/b3dm/tileset.json',
        url: '//zzgis.com/cdn/3d/3dtiles/mapbox_3dtiles_samples/composite/tileset.json',
        url: '//zzgis.com/cdn/3d/3dtiles/stadium_all/tileset.json',
        url: '//zzgis.com/cdn/3d/3dtiles/AzrieliTLV/tileset.json',
        // url: '//zzgis.com/cdn/3d/3dtiles/msl-dingo-gap/0528_0260184_to_s64o256/0528_0260184_to_s64o256/0528_0260184_to_s64o256_tileset.json',
        url: '//zzgis.com/cdn/3d/3dtiles/AzrieliTLV/tileset.json',
        url: '//zzgis.com/cdn/3d/3dtiles/taichung/tileset.taichung.json',
        url: '//zzgis.com/cdn/3d/3dtiles/40866/tileset.json',
      });

      tileset.readyPromise
        .then(function(tileset) {
          scene.primitives.add(tileset);
          viewer.zoomTo(tileset, new Cesium.HeadingPitchRange(0.0, -0.5, tileset.boundingSphere.radius * 2.0));
        })
        .otherwise(function(error) {
          console.error(error);
        });
        
      vm.setHeight = function(height) {
        height = Number(height);
        if (isNaN(height)) {
          return;
        }
        var cartographic = Cesium.Cartographic.fromCartesian(tileset.boundingSphere.center);
        var surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, 0.0);
        var offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, height);
        var translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3());
        tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
      };
    }
  }
});
</script>

<style lang="less">
.cesium-viewer-wapper {
  .tool-box-left-top {
    display: flex;
    button {
      min-width: 5em;
      background-color: rgba(0, 0, 0, 0.75);
      border-color: rgba(0, 0, 0, 0);
      cursor: pointer;
      transition: all 500ms;
      &:hover {
        opacity: 0.75;
      }
      &.active {
        background-color: rgba(0, 20, 200, 0.75);
      }
    }
  }
}
</style>
